using System;
using System.Data;
using System.Data.SqlClient;
using DataAccessLayer;
using EntityObjectLayer;

namespace BusinessLogicLayer
{
    public class BLManageUsers
    {
        #region Constructor

        #endregion

        #region Load User List

        /// <summary >
        /// ham thuc hien viec load danh sach cac user
        /// </summary>
        /// <returns>bang danh sanh user</returns>

        //o day khai bao bien usn roi lai ko dung?
        //dang bi loi DataTable belong to another dataset
        public DataSet loadUserList(BEUsers beusers) 

        {
            //DataSet ds = new DataSet();
            SqlParameter usn = new SqlParameter("@UserName", SqlDbType.VarChar, 30);

            usn.Value = beusers.sUserName;
            DataTable dt = Common.GetFromDataTableWithPaging("pr_tblUsers_Select", 0, 10);
            //ds.Tables.Add(dt);

            return dt.DataSet;
        }

        #endregion

        #region Reset Password

        /// <summary>
        /// ham thuc hien viec reset password
        /// </summary>
        
        public int resetPwd(BEUsers beusers) 
        {
            try
            {
                SqlParameter usn = new SqlParameter("@UserName", SqlDbType.VarChar, 30);
                usn.Value = beusers.sUserName;
                SqlParameter usn_v2 = new SqlParameter("@UserName", SqlDbType.VarChar, 30);
                usn_v2.Value = beusers.sUserName;
                SqlDataReader dr = Common.GetFromDataReader("pr_Session_SelectAll", usn_v2);
                if (dr.HasRows) return 2;
                Common.Execute("pr_tblUsers_Update", usn);
                return 1;
            }
            catch (Exception)
            {
                throw;
            }
        }

        #endregion

        #region Delete A User

        /// <summary>
        /// ham thuc hien viec xoa 1 user
        /// </summary>

        
        public int delUser(BEUsers beu) 
        {
            SqlParameter usn = new SqlParameter("@UserName", SqlDbType.VarChar, 30);
            usn.Value = beu.sUserName;
            SqlParameter usn_v2 = new SqlParameter("@UserName", SqlDbType.VarChar, 30);
            usn_v2.Value = beu.sUserName;
            SqlDataReader dr = Common.GetFromDataReader("pr_Session_SelectAll", usn_v2);
            if (dr.HasRows) return 2;
            Common.Execute("pr_tblUsers_Delete", usn);
            return 1;
        }

        #endregion

        #region Add New User

        /// <summary>
        /// ham thuc hien viec nhap them 1 user moi
        /// </summary>

        
        public int addNewUser(BEUsers beu) 
        {
            try
            {
                SqlParameter usn = new SqlParameter("@UserName", SqlDbType.VarChar, 30);
                usn.Value = beu.sUserName;
                SqlParameter pwd = new SqlParameter("@pass", SqlDbType.Char, 30);
                pwd.Value = beu.sPass;
                SqlParameter perm = new SqlParameter("@Permission", SqlDbType.Char, 30);
                perm.Value = beu.sPermission;
                Common.Execute("pr_tblUsers_Insert", usn, pwd, perm);
                return 1;
            }
            catch (Exception)
            {
                throw;
            }
        }

        #endregion

        #region Change Password

        /// <summary>
        /// ham thuc hien viec thay doi mat khau nguoi dung
        /// </summary>

        public int changePwd(BEUsers beusers,String newPwd)

        {
            try
            {
                SqlParameter usn = new SqlParameter("@UserName", SqlDbType.VarChar, 30);
                usn.Value = beusers.sUserName;
                SqlParameter pwd = new SqlParameter("@PassOld", SqlDbType.Char, 30);
                pwd.Value = beusers.sPass;
                SqlParameter newpassword = new SqlParameter("@PassNew", SqlDbType.Char, 30);
                newpassword.Value = newPwd;
                Common.Execute("pr_tblUsers_Update", usn, pwd, newpassword);
                return 1;
            }
            catch
            {
                return 0;
            }
            
        }

        #endregion

        #region Check User

        /// <summary>
        /// ham kiem tra de xac thuc user
        /// </summary>

        public int checkUser(BEUsers beusers) 

        {
            SqlParameter usn = new SqlParameter("@UserName", SqlDbType.VarChar, 30);
            usn.Value = beusers.sUserName;
            SqlDataReader dr = Common.GetFromDataReader("pr_tblUsers_SelectOne", usn);

            if (dr.HasRows) return 0;
            return 1;
        }

        #endregion

        #region Search User List

        /// <summary >
        /// ham thuc hien viec load danh sach cac user khi search
        /// </summary>
        /// <returns>bang danh sanh user</returns>
        public DataSet searchUserList(BEUsers beusers)
        {
            //DataSet ds = new DataSet();
            try
            {
                SqlParameter usn = new SqlParameter("@UserName", SqlDbType.VarChar, 30);

                usn.Value = beusers.sUserName;
                DataTable dt = Common.GetFromDataTableWithPaging("pr_tblUsers_Search", 0, 10, usn);
                //ds.Tables.Add(dt);
                return dt.DataSet;
            }
            catch (Exception e)
            {
                throw;
            }
        }

        #endregion
    }
}